package com.jxbeidou.system.user.controller;

import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.core.entity.SecurityUserVO;
import com.jxbeidou.system.api.user.dto.UserDTO;
import com.jxbeidou.system.user.convert.UserConvert;
import com.jxbeidou.system.user.entity.UserDO;
import com.jxbeidou.system.user.query.UserQuery;
import com.jxbeidou.system.user.service.UserService;
import com.jxbeidou.system.user.vo.UserVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
* 登录用户表
*
* @author 杜晓祥 
* @since 1.0.0 2023-12-14
*/
@RestController
@RequestMapping("/sys/user")
@AllArgsConstructor
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/page")
    public R<PageResult<UserVO>> page(@RequestBody @Valid UserQuery query){
        PageResult<UserVO> page = userService.page(query);

        return R.success(page);
    }

    @GetMapping("/findByUsername")
    R<SecurityUserVO> findByUsername (@RequestParam("username") String username) {
        return R.success(userService.loadUsername(username));
    }

    @GetMapping("/selectByUsername")
    public R<UserDTO> selectByUsername (@RequestParam("username") String username) {
        return R.success(userService.selectUsername(username));
    }

    @GetMapping("/{id}")
    public R<UserVO> get(@PathVariable("id") Long id){
        UserDO entity = userService.getById(id);
        return R.success(UserConvert.INSTANCE.convert(entity));
    }

    @GetMapping("/current")
    public R<UserVO> findByCurrent() {
        UserVO userVO = new UserVO();
        return R.success(userVO);
    }

    @PostMapping
    public R<String> save(@RequestBody UserVO vo){
        userService.save(vo);

        return R.success();
    }

    @PutMapping
    public R<String> update(@RequestBody @Valid UserVO vo){
        userService.update(vo);

        return R.success();
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") Long id){
        userService.delete(Arrays.asList(id));

        return R.success();
    }

    @DeleteMapping("/delete")
    public R<String> delete(@RequestBody List<Long> idList){
        userService.delete(idList);

        return R.success();
    }
}